﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Reflection;

namespace NewTools
{
    internal class ParseIni
    {
        private readonly string filePath = "\\Resource\\config\\cmd_default.ini";
        private string runFilePath = "\\Resource\\config\\run_default.ini";
        private readonly string sysFilePath = "\\Resource\\config\\sys.ini";
        //声明读写INI文件的API函数
        [DllImport("kernel32")]
        private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retValue, int size, string filePath);

        private static ParseIni sparseIni = null;

        public static ParseIni GetInstance()
        {
            if (null == sparseIni)
                sparseIni = new ParseIni();
            return sparseIni;
        }
        
        //类的构造函数，传递INI文件名
        public ParseIni()
        {
            filePath = System.Environment.CurrentDirectory + filePath;
            runFilePath = System.Environment.CurrentDirectory + runFilePath;
            sysFilePath = System.Environment.CurrentDirectory + sysFilePath;
        }


        public string DefaultCommandFunction_Get(Int32 index)
        {
            return ReadIniData("cmd_" + Convert.ToString(index), "function", "", filePath);
        }

        /**
         *
         *
         */
        public Int32 DefaultCommandNumber_Get()
        {
            string num = ReadIniData("number", "num", "", filePath);
            return Convert.ToInt32(num);
        }

        public Int32 AutoRunCommandNumber_Get(string floderName)
        {
            this.runFilePath = FileOperation.GetInstance().GetAutoRunFileName(floderName);
            string num = ReadIniData("number", "num", "", runFilePath);
            return Convert.ToInt32(num);
        }

        public string [] DefaultCommandName_Get()
        {
            Int32 num = DefaultCommandNumber_Get();
            string [] temp = new string[num];
            
            for (Int32 i = 0; i < num; i++) 
            {
                string command = ReadIniData("cmd_" + Convert.ToString(i),"name","",filePath);
                temp[i] = command;
            }
            return temp;
        }

        public byte[] DefaultCommandById_Get(Int32 index)
        {
            string command = ReadIniData("cmd_" + Convert.ToString(index), "cmd", "", filePath);
            byte[] sendCmd = new byte[12];

            if (command != null) {
                string[] commandList = command.Split(' ');
                for (Int32 i = 0; i < commandList.Length; i++)
                {   
                    sendCmd[i] = Convert.ToByte(commandList[i], 16);
                }
            }
            return sendCmd;
        }


        public byte[] AutoRunCommandById_Get(Int32 index,string floderName)
        {
            this.runFilePath = FileOperation.GetInstance().GetAutoRunFileName(floderName);
            string command = ReadIniData("cmd_" + Convert.ToString(index), "cmd", "", runFilePath);
            byte[] sendCmd = new byte[12];

            if (command != null)
            {
                string[] commandList = command.Split(' ');
                for (Int32 i = 0; i < commandList.Length; i++)
                {
                    sendCmd[i] = Convert.ToByte(commandList[i], 16);
                }
            }
            return sendCmd;
        }

        public string DefaultStringCommandById_Get(Int32 index)
        { 
            return ReadIniData("cmd_" + Convert.ToString(index), "cmd", "", filePath);
        }

        public string DefaultStringCmdNameById_Get(Int32 index)
        {
            return ReadIniData("cmd_" + Convert.ToString(index), "name", "", filePath);
        }

        public string AutoRunStringCommandById_Get(Int32 index,string floderName)
        {
            this.runFilePath = FileOperation.GetInstance().GetAutoRunFileName(floderName);
            return ReadIniData("cmd_" + Convert.ToString(index), "cmd", "", runFilePath);
        }

        public string AutoRunStringCommandNameById_Get(Int32 index,string floderName)
        {
            this.runFilePath = FileOperation.GetInstance().GetAutoRunFileName(floderName);
            return ReadIniData("cmd_" + Convert.ToString(index), "name", "", runFilePath);
        }

        public Int32 AutoRunSleepValue_Get(Int32 index,string floderName)
        {
            this.runFilePath = FileOperation.GetInstance().GetAutoRunFileName(floderName);
            string sleep = ReadIniData("cmd_" + Convert.ToString(index), "sleep", "", runFilePath);
            return Convert.ToInt32(sleep);
        }

        public bool TimmingNumber_Write(string num)
        {
            return WriteIniData("Chroma", "TimmingNum",num.ToString(),sysFilePath);
        }

        public string TimmingNumber_Read()
        {
            return ReadIniData("Chroma", "TimmingNum","",sysFilePath);
        }

        public bool PatternNumber_Write(string num)
        {
            return WriteIniData("Chroma", "PatternNum", num.ToString(), sysFilePath);
        }

        public string PatternNumber_Read()
        {
            return ReadIniData("Chroma", "PatternNum", "", sysFilePath);
        }

        public bool SignalSerialPortName_Write(string portName)
        {
            return WriteIniData("Chroma","Com",portName,sysFilePath);
        }

        public string SignalSerialPortName_Read()
        {
            return ReadIniData("Chroma", "Com","",sysFilePath);
        }

        public bool TVSerialPortName_Write(string portName)
        {
            return WriteIniData("Tv", "Com", portName, sysFilePath);
        }

        public string TVSerialPortName_Read()
        {
            return ReadIniData("Tv", "Com", "", sysFilePath);
        }

        public bool TvSerialPortBaudRate_Write(String baudRate)
        {
            return WriteIniData("Tv", "BaudRate", baudRate, sysFilePath);
        }

        public string TvSerialPortBaudRate_Read()
        {
            return ReadIniData("Tv","BaudRate","",sysFilePath);
        }

        public bool TvSerialPortDataBits_Write(String dataBits)
        {
            return WriteIniData("Tv","DataBits",dataBits,sysFilePath);
        }

        public string TvSerialPortDataBits_Read()
        {
            return ReadIniData("Tv","DataBits","",sysFilePath);
        }

        public bool TvSerialPortStopBits_Write(string stopBits)
        {
            return WriteIniData("Tv","StopBits",stopBits,sysFilePath);
        }

        public string TvSerialPortStopBits_Read()
        {
            return ReadIniData("Tv","StopBits","",sysFilePath);
        }

        public bool TvSerialPortParity_Write(string parity)
        {
            return WriteIniData("Tv","Parity",parity,sysFilePath);
        }

        public string TvSerialPortParity_Read()
        {
            return ReadIniData("Tv","Parity","",sysFilePath);
        }

        public bool CA410SerialPortName_Write(string portName)
        {
            return WriteIniData("Ca410", "Com", portName, sysFilePath);
        }

        public string CA410SerialPortName_Read()
        {
            return ReadIniData("Ca410", "Com", "", sysFilePath);
        }

        public bool ModelName_Write(string name)
        {
            return WriteIniData("Sys","Model",name,sysFilePath);
        }

        public string ModelName_Read()
        {
            return ReadIniData("Sys","Model","",sysFilePath);
        }

        private string ReadIniData(string section, string key, string defValue, string iniPath)
        {
            if (File.Exists(iniPath))
            {
                StringBuilder temp = new StringBuilder(128);
                GetPrivateProfileString(section, key, defValue, temp, 128, iniPath);
                return temp.ToString();
            }
            else
            {
                return String.Empty;
            }
        }

        private bool WriteIniData(string section, string key,string val,string filePath)
        {
            return WritePrivateProfileString(section, key, val, filePath);
        }
    }
}
